package com.xjgzinfo.usdp.web.rest;

import com.xjgzinfo.usdp.core.domain.PageWapper;
import com.xjgzinfo.usdp.core.domain.ResponsePage;
import com.xjgzinfo.usdp.core.domain.ResponseResult;
import com.xjgzinfo.usdp.service.BladeVisualCategoryQueryService;
import com.xjgzinfo.usdp.service.BladeVisualCategoryService;
import com.xjgzinfo.usdp.service.dto.BladeVisualCategoryCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualCategoryDTO;
import com.xjgzinfo.usdp.web.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.xjgzinfo.usdp.domain.BladeVisualCategory}.
 */
@RestController
@RequestMapping("/blade-visual")
public class CategoryResource {

    private static final String ENTITY_NAME = "AvueServer4jAppBladeVisualCategory";
    private final Logger log = LoggerFactory.getLogger(CategoryResource.class);
    private final BladeVisualCategoryService bladeVisualCategoryService;
    private final BladeVisualCategoryQueryService bladeVisualCategoryQueryService;
    @Value("${spring.application.name:usdp-app-unkown}")
    private String applicationName;

    public CategoryResource(BladeVisualCategoryService bladeVisualCategoryService, BladeVisualCategoryQueryService bladeVisualCategoryQueryService) {
        this.bladeVisualCategoryService = bladeVisualCategoryService;
        this.bladeVisualCategoryQueryService = bladeVisualCategoryQueryService;
    }

    /**
     * {@code POST  /category/save} : 创建实体 bladeVisualCategory.
     *
     * @param bladeVisualCategoryDTO the bladeVisualCategoryDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new bladeVisualCategoryDTO, or with status {@code 400 (Bad Request)} if the bladeVisualCategory has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/category/save")
    public ResponseResult<BladeVisualCategoryDTO> createBladeVisualCategory(@Valid @RequestBody BladeVisualCategoryDTO bladeVisualCategoryDTO) throws URISyntaxException {
        log.debug("REST request to save BladeVisualCategory : {}", bladeVisualCategoryDTO);
        if (bladeVisualCategoryDTO.getId() != null) {
            return new ResponseResult(400, null, "ID已存在", false);
        }
        BladeVisualCategoryDTO result = bladeVisualCategoryService.save(bladeVisualCategoryDTO);
        if (result.getId() != null) {
            return new ResponseResult(200, result, "创建成功", true);
        } else {
            return new ResponseResult(400, result, "创建失败", false);
        }
    }

    /**
     * {@code PUT  /category/update} : 修改实体 bladeVisualCategory.
     *
     * @param bladeVisualCategoryDTO the bladeVisualCategoryDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated bladeVisualCategoryDTO,
     * or with status {@code 400 (Bad Request)} if the bladeVisualCategoryDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the bladeVisualCategoryDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/category/update")
    public ResponseResult<BladeVisualCategoryDTO> updateBladeVisualCategory(@Valid @RequestBody BladeVisualCategoryDTO bladeVisualCategoryDTO) throws URISyntaxException {
        log.debug("REST request to update BladeVisualCategory : {}", bladeVisualCategoryDTO);
        if (bladeVisualCategoryDTO.getId() == null) {
            return new ResponseResult(200, null, "ID不能为空", false);
        }
        BladeVisualCategoryDTO result = bladeVisualCategoryService.save(bladeVisualCategoryDTO);
        if (result.getId() != null) {
            return new ResponseResult(200, result, "创建成功", true);
        } else {
            return new ResponseResult(400, result, "创建失败", false);
        }
    }

    /**
     * {@code GET  /categories} : 获取全部实体 bladeVisualCategories.
     *
     * @param pageable the pagination information.
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of bladeVisualCategories in body.
     */
    @GetMapping("/categories")
    public ResponseEntity<List<BladeVisualCategoryDTO>> getPageBladeVisualCategories(BladeVisualCategoryCriteria criteria, Pageable pageable) {
        log.debug("REST request to get BladeVisualCategories by criteria: {}", criteria);
        Page<BladeVisualCategoryDTO> page = bladeVisualCategoryQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * {@code GET  /category/list} : 获取全部实体 bladeVisualCategories.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of bladeVisualCategories in body.
     */
    @GetMapping("/category/list")
    public ResponseResult<List<BladeVisualCategoryDTO>> getAllBladeVisualCategories(BladeVisualCategoryCriteria criteria, Pageable pageable) {
        log.debug("REST request to get BladeVisualCategories by criteria: {}", criteria);
        List<BladeVisualCategoryDTO> list = bladeVisualCategoryQueryService.findByCriteria(criteria);
        return new ResponseResult(200, list, "", true);
    }

    /**
     * {@code GET  /category/count} : count all the bladeVisualCategories.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the count in body.
     */
    @GetMapping("/category/count")
    public ResponseEntity<Long> countBladeVisualCategories(BladeVisualCategoryCriteria criteria) {
        log.debug("REST request to count BladeVisualCategories by criteria: {}", criteria);
        return ResponseEntity.ok().body(bladeVisualCategoryQueryService.countByCriteria(criteria));
    }

    /**
     * {@code GET  /category/detail} : ID查询实体 bladeVisualCategory.
     *
     * @param id the id of the bladeVisualCategoryDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the bladeVisualCategoryDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/category/detail")
    public ResponseResult<BladeVisualCategoryDTO> getBladeVisualCategory(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long queryId = id != null ? id : equalsId;
        log.debug("REST request to get BladeVisualCategory : {}", queryId);
        Optional<BladeVisualCategoryDTO> bladeVisualCategoryDTO = bladeVisualCategoryService.findOne(queryId);
        if (bladeVisualCategoryDTO.isPresent()) {
            return new ResponseResult(200, bladeVisualCategoryDTO.get(), "", true);
        } else {
            return new ResponseResult(404, null, "未查询到数据", false);
        }
    }

    /**
     * {@code DELETE  /category/remove} : ID删除实体 bladeVisualCategory.
     *
     * @param id the id of the bladeVisualCategoryDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @PostMapping("/category/remove")
    public ResponseResult<Void> deleteBladeVisualCategory(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long deleteId = id != null ? id : equalsId;
        log.debug("REST request to delete BladeVisualCategory : {}", deleteId);
        bladeVisualCategoryService.delete(deleteId);
        return new ResponseResult(200, null, "删除成功", true);
//        Optional<BladeVisualCategoryDTO> bladeVisualCategoryDTOOpt = bladeVisualCategoryService.findOne(deleteId);
//        if (bladeVisualCategoryDTOOpt.isPresent()) {
//            BladeVisualCategoryDTO bladeVisualCategoryDTO = bladeVisualCategoryDTOOpt.get();
//            if (bladeVisualCategoryDTO.getIsDeleted() == 0) {
//                bladeVisualCategoryDTO.setIsDeleted(1);
//                bladeVisualCategoryService.save(bladeVisualCategoryDTO);
//            } else {
//                bladeVisualCategoryService.delete(deleteId);
//            }
//            return new ResponseResult(200, null, "删除成功", true);
//        } else {
//            return new ResponseResult(404, null, "未查询到数据", false);
//        }
    }
}
